package com.qilin.hscs.udp;

import java.io.IOException;
import java.net.*;

/**
 * Created by 10188 on 2018/4/26.
 */
public class UDPCliet {

    private static final int targetPort = 8888;
    private static final String targetHost = "192.168.31.141";
    public static void main(String[] args) {
        try {
            DatagramSocket datagramSocket = new DatagramSocket();
            String x = "QiLin";
            byte b[] = x.getBytes();
            DatagramPacket datagramPacket = new DatagramPacket(b, b.length);
            InetAddress inetAddress=InetAddress.getByName(targetHost);
            if (inetAddress.isReachable(3000)) {
                datagramPacket.setSocketAddress(new InetSocketAddress(inetAddress,targetPort));
                datagramSocket.send(datagramPacket);
                DatagramPacket res = new DatagramPacket(new byte[1024], 1024);
                datagramSocket.receive(res);
                byte [] data=res.getData();
                System.out.println(new String(data,0,data.length,"ASCII"));
            }else {
                System.out.println("数据包可能无法到达目的地，请联系核对服务端地址");
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
